package com.manor.im.dialog

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.inputmethod.EditorInfo
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.FullScreenPopupView
import com.lxj.xpopup.util.KeyboardUtils
import com.manor.common.http.HttpSignCallback
import com.manor.common.ktx.toBeanList
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.StringUtil
import com.manor.common.utils.str
import com.manor.im.R
import com.manor.im.activity.HChatRoomActivity
import com.manor.im.bean.IMsgBean
import com.manor.im.http.ImHttpUtil
import com.tencent.imsdk.v2.V2TIMConversation
import kotlinx.android.synthetic.main.dialog_i_msg_search.view.*

/**
 * 搜索群聊


 * @date  2020/7/24  17:52
 * @version 1.0
 */
class IMSGSearchDialog(context: Context) : FullScreenPopupView(context) {

    private var mAdapter: MAdapter? = null


    companion object {
        fun load(context: Context) {
            if (ClickUtil.canClick())
            XPopup.Builder(context)
                    .asCustom(IMSGSearchDialog(context))
                    .show()
        }
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_i_msg_search
    }

    override fun onCreate() {
        super.onCreate()
        mAdapter = MAdapter(R.layout.item_i_msg_search, mutableListOf())
        mRecycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mRecycler.adapter = mAdapter
        mAdapter?.setOnItemClickListener(itemClickListener)
        tvDismiss.setOnClickListener { dismiss() }
        etSearch.setOnEditorActionListener { v, actionId, event ->
            if (StringUtil.isNotEmpty(etSearch.str()))
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索的时候隐藏软键盘
                    KeyboardUtils.hideSoftInput(etSearch)
                    doSearch(etSearch.str())
                    // 在这里写搜索的操作,一般都是网络请求数据
                    return@setOnEditorActionListener true;
                }

            return@setOnEditorActionListener false;

        }
       // etSearch.addTextChangedListener(editListener)
    }

    private fun doSearch(key: String) {
        ImHttpUtil.getIMsgSearch(key, object : HttpSignCallback() {
            override fun onSuccess(code: Int, msg: String?, info: String?) {
                if (code == 0 && info != null && info.isNotEmpty()) {
                    val mBean = info.toBeanList<IMsgBean>()
                    mAdapter?.setNewInstance(mBean)
                    mAdapter?.notifyDataSetChanged()

                }
            }

        })
    }


    private val editListener = object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            s?.run {
                doSearch(this.toString())
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    }

    private val itemClickListener = OnItemClickListener { adapter, _, pos ->
        val item = adapter.data[pos] as IMsgBean?
        item?.run {
            if (ClickUtil.canClick())
                HChatRoomActivity.forward(context, this.id, this.name, this.img,
                        if (this.type == "1") V2TIMConversation.V2TIM_GROUP else V2TIMConversation.V2TIM_C2C)

        }
    }


    private class MAdapter(layoutResId: Int, data: MutableList<IMsgBean>?)
        : BaseQuickAdapter<IMsgBean, BaseViewHolder>(layoutResId, data) {

        override fun convert(holder: BaseViewHolder, item: IMsgBean) {
            item?.run {

                Glide.with(holder.getView<ImageView>(R.id.ivImage))
                        .load(this.img)
                        .circleCrop()
                        .into(holder.getView(R.id.ivImage))

                holder.setText(R.id.tvTitle, name)


            }
        }

    }

}